<?php

require_once ( LIB_DIR.'search/moviesearch.php');
/**
 * do search 
 *
 * @author Jensen
 */
class advanced_search extends AbstractAction {

    public function run() {
        global $TPL, $filters;
        $page = intval($_GET['p']);
        if (empty($page)) {
            $page = 1;
        }

        $sortfield = cleanInput($_GET['sortby']);
        $keyword = cleanInput($_GET['q']);
        $isdesc =  intval($_GET['orderby']) == 0?true:false;
        
        if( debug ){
            $search = new moviesearch( "42.121.129.20", 9312 );
        }
        else{
            $search = new moviesearch();
        }
        
        $plainKeyword = $keyword;
        $TPL->assign('keyword', $plainKeyword);
        
        $TPL->assign('filters', $this->getFilterValues() );
        
        $additionalKeyword = array();
        if( !empty($keyword) ){
            $additionalKeyword['@(title,othername,actors,director,englishname)'] = $keyword;
        }
        // assemble other search field : 地区 类型 演员 导演 编剧
        if( !empty($_GET['location']) ){
            $additionalKeyword['@location'] = $search->CleanKeyword($_GET['location']);
        }
        if( !empty($_GET['type']) ){
            $additionalKeyword['@type'] = $search->CleanKeyword($_GET['type']);
        }
        if( !empty($_GET['actor']) ){
            $additionalKeyword['@actors'] = $search->CleanKeyword($_GET['actor']);
        }
        if( !empty($_GET['director']) ){
            $additionalKeyword['@director'] = $search->CleanKeyword($_GET['director']);
        }
        if( !empty($_GET['story']) ){
            $additionalKeyword['@story'] = $search->CleanKeyword($_GET['story']);
        }
        
        //assemble keyword
        foreach( $additionalKeyword as $key=>$value ) {
            $keyword .= " $key $value";
        }
        
        // condition filter
        $conditions = array();
        if( !empty($_GET['movietype']) ){
            $conditions['movietype'] = array( intval($_GET['movietype']) );
        }
        $sortfield="updatetime";
        $isdesc = true;
        $result = $search->MultiFilterAndSortQuery( $keyword, $conditions, $sortfield, $page, $isdesc );

        // if there is only one result, go to detail page directly
        if(  $result['total'] == 1 ){
            if(debug){
                header("Location: index.php?action=item_new&id=".  $result['matchIds'][0]);
            }
            else{
                header("Location: /item_new/".  $result['matchIds'][0]);
            }
            exit;
        }
        else if( $result['total'] > 0 ) {
            //!!!!shit here, need to sort the result with extra sql statement : ORDER BY FIELD(id, ".$strIds.")
            //and then do sort here
            $movieDao = $this->getDao("MovieDAO");
            $dbRes = $movieDao->GetMovieInfoByIdList( $result['matchIds'], MOVIE_PERPAGE );
            
            if( ! empty( $plainKeyword )){
                $highLighRes = array();
                foreach( $dbRes as $row ) {
                    $row['title_plain'] = $row['title'];
                    //assemble field to highlight : shopname, shop description, nick, location, paymethod, tag, feature
                    $arrToHighlight = array
                    (
                        //hard code here, need to be modified later
                        'title' => $row['title'],
                        'actors' => $row['actors'],
                        'englishname' => $row['englishname'],
                        'othername' => $row['othername']
                    );
                    $row = array_merge( $row, $search->BuildHighLight( $plainKeyword, $arrToHighlight ) );
                    $highLighRes[] = $row;
                }

                foreach( $highLighRes as &$item ){
                    // set large images
                    $item['largeImgUrl'] = $this->getLargeImgUrl($item['imgUrl']);
                }
                $TPL->assign('items', $highLighRes);
            }
            else{
                foreach( $dbRes as &$item ){
                    // set large images
                    $item['largeImgUrl'] = $this->getLargeImgUrl($item['imgUrl']);
                }
                $TPL->assign('items', $dbRes);
            }
            
            // pager information
            if (debug) {
                $pager = new Pager('index.php?action=movie_list', $result['total_found'], MOVIE_PERPAGE, $page, 'p');
            } else {
                $pager = new Pager($_SERVER['REQUEST_URL'], $result['total_found'], MOVIE_PERPAGE, $page, 'p');
            }
            $TPL->assign('pager', $pager->getBar());
            
            // save search history
            $searchHistoryDao = $this->getDao("SearchHistoryDAO");
            if( !empty($plainKeyword) ){
                $searchdata = array();
                $searchdata['keyword'] = $plainKeyword;
                $searchdata['searchcount'] = 1;
                if( $searchHistoryDao->Add( $searchdata ) === false){
                    $searchHistoryDao->Update( $plainKeyword );
                }
            }
            
            // get search history
            $searchhistory = $searchHistoryDao->GetAllItems();
            $TPL->assign('searchhistory', $searchhistory );
        }
        else{
            $TPL->assign('items', array() );
        }
        
        // set search filter value
        $TPL->assign('location', $_GET['location']);
        $TPL->assign('detailtype', $_GET['type']);
        $TPL->assign('movietype', $this->getMovieTypeNameById($_GET['movietype']));
        
        $TPL->assign('current_page', 'movie_list');
        $TPL->display('theme_v2/list.tpl');
    }

}

?>
